

class CanvasHelper {
  //传入画布
  constructor() {
  }

  SetContext(ctx) {
    this.ctx = ctx
  }

  //画线
  DrawLine(x1, y1, x2, y2) {
    this.ctx.beginPath()
    this.ctx.moveTo(x1, y1);
    this.ctx.lineTo(x2, y2);
    this.ctx.stroke();
  }

  //画矩形，x,y是左上角坐标, width,height是矩形的宽和高
  DrawRect(x, y, width, height) {
    this.ctx.strokeRect(x, y, width, height);
  }

  //画矩形，x,y是矩形中心店坐标，width,height是矩形的宽和高
  DrawRectByCenter(x, y, width, height) {
    this.ctx.strokeRect(x - width/2, y - height/2, width, height);
  }

  //画带颜色的矩形
  DrawRectByCenterWithColor(x, y, width, height, strokeColor, fillColor) {
    this.ctx.save()  //保存beginPath之前的状态
    this.ctx.beginPath()
    this.ctx.strokeStyle = strokeColor
    this.ctx.fillStyle = fillColor
    this.ctx.rect(x - width/2, y - height/2, width, height)
    this.ctx.stroke()
    this.ctx.fill()
    this.ctx.closePath()
    this.ctx.restore()  //恢复beginPath之前的状态
  }

  //填充矩形
  FillRectByCenter(x, y, width, height, fillColor) {
    this.ctx.save()  //保存beginPath之前的状态
    this.ctx.beginPath()
    this.ctx.fillStyle = fillColor
    this.ctx.rect(x - width/2, y - height/2, width, height)
    this.ctx.fill()
    this.ctx.closePath()
    this.ctx.restore()  //恢复beginPath之前的状态
  }

  //画实心圆
  DrawSolidCircle(x, y, radius) {
    this.ctx.beginPath();
    this.ctx.arc(x, y, radius, 0, 2*Math.PI);
    this.ctx.fill();
  }

  //画单个文字
  DrawSingleWord(single_word, center_x, center_y, font_size) {
    if (typeof single_word != "string") {
      throw "draw single word: not string"
    }
    if (single_word.length != 1) {
      throw "draw single word: length is not 1"
    }
    this.ctx.font = font_size + "px Monospaced Number,Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif"
    this.ctx.fillText(single_word, center_x - font_size/2, center_y + font_size/2);
  }
}

module.exports = {
  CanvasHelper
}
